package com.punch.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.dto.system.HonorAdviserDTO;
import com.punch.common.exception.BusinessException;
import com.punch.common.page.PageInfo;
import com.punch.common.query.syste.HonorAdviserQueryInfo;
import com.punch.common.simple.redis.utils.UserUtil;
import com.punch.common.tools.BasePage;
import com.punch.common.utils.ImageUtil;
import com.punch.common.vo.system.HonorAdviserVO;
import com.punch.system.exception.SysExceptionEnum;
import com.punch.system.mapper.HonorAdviserMapper;
import com.punch.system.model.HonorAdviser;
import com.punch.system.service.HonorAdviserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;

/**
 * <p>
 * 名誉顾问表 服务实现类
 * </p>
 *
 * @author wjj
 * @since 2020-12-14
 */
@Service
public class HonorAdviserServiceImpl extends ServiceImpl<HonorAdviserMapper, HonorAdviser> implements HonorAdviserService {

    /**
     * 名誉顾问列表
     *
     * @param queryInfo 查询条件
     * @return PageInfo<HonorAdviserVO>
     */
    @Override
    public PageInfo<HonorAdviserVO> listHonorAdvise(HonorAdviserQueryInfo queryInfo) {
        queryInfo.setSortStr("update_time");
        return BasePage.queryBasePage(queryInfo, params -> this.getBaseMapper().queryCount(params),
                params -> {
                    List<HonorAdviserVO> retList = getBaseMapper().queryList(params);
                    retList.forEach(honorAdviserVO -> {
                        if (honorAdviserVO.getStatus() == 0) {
                            honorAdviserVO.setUpdateTime(null);
                        }
                    });
                    return retList;
                });
    }

    /**
     * 保存名誉顾问
     *
     * @param honorAdviserDTO 提交表单
     * @return id
     */
    @Override
    public Integer saveHonorAdvise(HonorAdviserDTO honorAdviserDTO) {
        HonorAdviser honorAdviser = new HonorAdviser();
        BeanUtil.copyProperties(honorAdviserDTO, honorAdviser);
        Long userId = UserUtil.getLoginUserId();
        honorAdviser.setCreateId(userId);
        //名誉顾问介绍
        if (CollUtil.isEmpty(honorAdviserDTO.getImageList())) {
            honorAdviser.setIntroduceImg("");
        } else {
            StringJoiner sj = new StringJoiner(",");
            honorAdviserDTO.getImageList().forEach((img) -> sj.add(ImageUtil.removePrefix(img)));
            honorAdviser.setIntroduceImg(sj.toString());
        }
        //处理名誉顾问前缀
        if (StrUtil.isNotEmpty(honorAdviserDTO.getAdviserImg())) {
            String img = ImageUtil.removePrefix(honorAdviserDTO.getAdviserImg());
            honorAdviser.setAdviserImg(img);
        }
        if (!save(honorAdviser)) {
            throw new BusinessException(SysExceptionEnum.DATA_INSERT_EXCEPTION);
        }
        return honorAdviser.getId();
    }

    /**
     * 修改名誉顾问
     *
     * @param honorAdviserDTO 提交表单
     * @return id
     */
    @Override
    public Integer updateHonorAdvise(HonorAdviserDTO honorAdviserDTO) {
        HonorAdviser honorAdviser = new HonorAdviser();
        BeanUtil.copyProperties(honorAdviserDTO, honorAdviser);
        honorAdviser.setId(honorAdviserDTO.getHonorAdviserId());
        //名誉顾问介绍
        if (CollUtil.isNotEmpty(honorAdviserDTO.getImageList())) {
            StringJoiner sj = new StringJoiner(",");
            honorAdviserDTO.getImageList().forEach((img) -> sj.add(ImageUtil.removePrefix(img)));
            honorAdviser.setIntroduceImg(sj.toString());
        } else {
            honorAdviser.setIntroduceImg("");
        }
        //处理名誉顾问前缀
        if (StrUtil.isNotEmpty(honorAdviserDTO.getAdviserImg())) {
            String img = ImageUtil.removePrefix(honorAdviserDTO.getAdviserImg());
            honorAdviser.setAdviserImg(img);
        }
        if (!updateById(honorAdviser)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return honorAdviser.getId();
    }

    /**
     * 名誉顾问详情
     *
     * @param honorAdviserId honorAdviserId
     * @return HonorAdviserVO
     */
    @Override
    public HonorAdviserVO getHonorAdviser(Integer honorAdviserId) {
        HonorAdviser honorAdviserEX = getById(honorAdviserId);
        Optional.ofNullable(honorAdviserEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        HonorAdviserVO honorAdviserVO = new HonorAdviserVO();
        BeanUtil.copyProperties(honorAdviserEX, honorAdviserVO);
        if (StrUtil.isNotEmpty(honorAdviserEX.getAdviserImg())) {
            honorAdviserVO.setAdviserImg(ImageUtil.addPrefix(honorAdviserEX.getAdviserImg()));
        }
        //介绍图片
        if (StrUtil.isNotEmpty(honorAdviserEX.getIntroduceImg())) {
            String[] split = honorAdviserEX.getIntroduceImg().split(",");
            List<String> imgList = new ArrayList<>();
            for (String img : split) {
                imgList.add(ImageUtil.addPrefix(img));
            }
            honorAdviserVO.setImageList(imgList);
        }
        honorAdviserVO.setHonorAdviserId(honorAdviserId);
        return honorAdviserVO;
    }

    /**
     * 删除名誉顾问
     *
     * @param honorAdviserId honorAdviserId
     * @return id
     */
    @Override
    public Integer deleteHonorAdviser(Integer honorAdviserId) {
        HonorAdviser honorAdviserEX = getById(honorAdviserId);
        Optional.ofNullable(honorAdviserEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        //已经展示的不能删除,需要下架
        if (honorAdviserEX.getStatus() == 1) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        if (!removeById(honorAdviserId)) {
            throw new BusinessException(SysExceptionEnum.DATA_DELETION_EXCEPTION);
        }
        return honorAdviserId;
    }

    /**
     * 修改名誉顾问状态
     *
     * @param honorAdviserId honorAdviserId
     * @param status         状态 0-不显示 1-显示
     * @return honorAdviserId
     */
    @Override
    public Integer updateHonorAdviserStatus(Integer honorAdviserId, Integer status) {
        HonorAdviser honorAdviserEX = getById(honorAdviserId);
        Optional.ofNullable(honorAdviserEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        if (honorAdviserEX.getStatus().equals(status)) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        HonorAdviser honorAdviser = new HonorAdviser();
        honorAdviser.setStatus(status);
        honorAdviser.setId(honorAdviserId);
        if (!updateById(honorAdviser)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return honorAdviserId;
    }
}
